package com.jianyitech.asr.controller;


import com.jianyitech.asr.dtos.ConfigureDto;
import com.jianyitech.asr.service.AsrRuleService;
import com.jianyitech.asr.service.LoadConfigureService;
import com.jianyitech.asr.vo.TenantVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jianyitech.asr.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * 加载配置
 */
@CrossOrigin
@RestController
@RequestMapping(value="/api/v1.0/loadConfigure")
public class LoadConfigureController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(LoadConfigureController.class);

    @Autowired
    LoadConfigureService loadConfigureService;

    @Autowired
    AsrRuleService asrRuleService;


    /**
     * 获取ip和配置信息
     */
    @GetMapping(value = "")
    public Result<ConfigureDto> loadConfigure(TenantVo vo) {
        Result<ConfigureDto> result = new Result<>();
        int code = -1;
        String message = null;
        try {
            check(vo);
            ConfigureDto data = loadConfigureService.loadConfigure(vo.getAppId());
            data.setIp(getIpAddr());
            result.setData(data);
            code = 0;
        } catch (Exception e) {
            message = e.getMessage();
            logger.error("loadConfigure  failed! subjectCode->{}", vo,e);
        } finally {
            result.setCode(code);
            result.setMessage(message);
            return result;
        }
    }

    /**
     * 通知客户端质检规则的变化
     * @return
     */
    @PostMapping(value="/rules/{tenantId}")
    public Result<String> loadRule(@PathVariable String tenantId,TenantVo vo){
        Result<String> result = new Result<>();
        int code = -1;
        String message = null;
        try{
            vo.setAppId(tenantId);
            check(vo);
            asrRuleService.noticeClient(vo.getAppId());
            code = 0;
            result.setData("ok");
        }catch (Exception e){
            message = e.getMessage();
            logger.error("[LoadAsrRuleException]: tenantId -> {}",vo.getAppId(),e);
        }finally {
            result.setCode(code);
            result.setMessage(message);
            return result;
        }

    }

}
